package tv.huan.media.service.impl;

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tv.huan.common.core.text.Convert;
import tv.huan.media.mapper.AlbumMapper;
import tv.huan.media.domain.Album;
import tv.huan.media.service.IAlbumService;

/**
 * 专辑Service业务层处理
 * 
 * @author admin
 * @date 2020-04-03
 */
@Service
public class AlbumServiceImpl implements IAlbumService 
{
    @Autowired
    private AlbumMapper albumMapper;

    /**
     * 查询专辑
     * 
     * @param id 专辑ID
     * @return 专辑
     */
    @Override
    public Album selectAlbumById(Integer id)
    {
        return albumMapper.selectAlbumById(id);
    }

    /**
     * 查询专辑列表
     * 
     * @param album 专辑
     * @return 专辑
     */
    @Override
    public List<Album> selectAlbumList(Album album)
    {
        return albumMapper.selectAlbumList(album);
    }

    /**
     * 新增专辑
     * 
     * @param album 专辑
     * @return 结果
     */
    @Override
    public int insertAlbum(Album album)
    {
        Date date = new Date();
        album.setCreateTime(date);
        return albumMapper.insertAlbum(album);
    }

    /**
     * 修改专辑
     * 
     * @param album 专辑
     * @return 结果
     */
    @Override
    public int updateAlbum(Album album)
    {
        return albumMapper.updateAlbum(album);
    }

    /**
     * 删除专辑对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteAlbumByIds(String ids)
    {
        return albumMapper.deleteAlbumByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除专辑信息
     * 
     * @param id 专辑ID
     * @return 结果
     */
    public int deleteAlbumById(Integer id)
    {
        return albumMapper.deleteAlbumById(id);
    }

    /**
     * 将专辑分配给审核小组
     *
     * @param albumIds 分配的专辑ID
     * @param teamId 审核小组ID
     * @return 结果
     */
    @Transactional
    public void assignTeam(String albumIds, Integer teamId) throws Exception{
        Integer[] albumIdArr = Convert.toIntArray(albumIds);
        Album album;
        for (Integer albumId : albumIdArr){
            Album selectAlbum = albumMapper.selectAlbumById(albumId);
            if (selectAlbum.getStatus() != 0){
                throw new Exception("存在已分配的专辑！");
            }
            album = new Album();
            album.setId(albumId);
            album.setTeamId(teamId);
            albumMapper.assignTeam(album);
        }
    }
}
